<?php 
	namespace Models;

	class Series
	{	
		private $app;
		function __construct($app){
			$this->app = $app;
		}

		function getAll(){
			$sql = "SELECT idseries series_id, series_type, series_name, series_season, series_count, series_link, series_date FROM series";
			return $this->app['db']->fetchAll($sql);
		}

		function getOne($id){
			$sql = "SELECT idseries series_id, series_type, series_name, series_season, series_count, series_link, series_date FROM series WHERE idseries = ? ";
			return $this->app['db']->fetchAssoc($sql, array( (int) $id ));	
		}

		function insert($data){
			$fields_name = array("series_type", "series_name", "series_season", "series_count", "series_link", "series_date");
			$arr_insert = array();
			foreach ($fields_name as $field) {
				if( isset( $data[$field] ) ){
					$arr_insert[$field] = $data['field'];
				}			
			}

			$this->app['db']->insert('series', $arr_insert);
			$id = $this->app['db']->lastInsertId();
			return array('series_id'=>$id);
		}

		function update($data){
			$id = $data['series_id'];
			$fields_name = array("series_type", "series_name", "series_season", "series_count", "series_link", "series_date");
			$arr_update = array();
			foreach ($fields_name as $field) {
				if( isset( $data[$field] ) ){
					$arr_update[$field] = $data['field'];
				}			
			}			
			$this->app['db']->update('series', $arr_update, array( 'idseries'=> $id ));
		}

		function delete($data){
			if( isset($data['series_id']) ){
				$this->app['db']->delete('series', array('idseries'=>$data['series_id'] ));
			}
		}

	}

?>